package org.example.utils.properties_utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * properties 工具类
 *
 * @Author: WangShengJie
 * @Date: 2025/4/18
 */
@Slf4j
public abstract class PropertiesUtils {

    /**
     * 根据 key，获取properties文件对应值
     *
     * @param key key
     * @return key为空或不存在，返回值为null
     */
    public static String getProperties(String key) {
        FileInputStream fileInputStream = null;
        Properties properties = new Properties();
        String value = "";
        try {
            fileInputStream = new FileInputStream("src/main/resources/demo.properties");
            properties.load(fileInputStream);
            value = properties.getProperty(key);
            log.info("读取properties文件-->{}={}", key, value);
        } catch (FileNotFoundException e) {
            log.error("读取properties文件-->失败！读取key:{}", key, e);
        } catch (IOException ioException) {
            log.error("读取properties文件-->失败！读取key:{}", key, ioException);
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return value;
    }

    /**
     * 根据 key，获取properties文件对应值
     *
     * @param keys keys
     * @return map
     */
    public static Map<String, String> getProperties(List<String> keys) {

        Map<String, String> map = new HashMap<>();
        if (keys == null || keys.size() <= 0) {
            return map;
        }
        FileInputStream fileInputStream = null;
        Properties properties = new Properties();
        String value = "";
        try {
            fileInputStream = new FileInputStream("src/main/resources/demo.properties");
            properties.load(fileInputStream);
            for (String key : keys) {
                value = properties.getProperty(key);
                value = StringUtils.isBlank(value) ? "" : value;
                map.put(key, value);
            }

            log.info("读取properties文件-->{}", map);
        } catch (FileNotFoundException e) {
            log.error("读取properties文件-->失败！读取keys:{}", keys, e);
        } catch (IOException ioException) {
            log.error("读取properties文件-->失败！读取key:{}", keys, ioException);
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return map;
    }

    /**
     * 设置properties
     * @param key key
     * @param value val
     * @return boolean
     */
    public static boolean saveProperties(String key, String value) {
        boolean flag = true;
        Properties properties = new Properties();
        FileOutputStream out = null;
        try {
            out = new FileOutputStream("src/main/resources/demo.properties");
            properties.setProperty(key, value);
            properties.store(out,"");
            log.warn("设置properties文件数据-->初始项：{}=【{}】", key, value);
        } catch (FileNotFoundException e) {
            flag = false;
            log.error("设置properties文件数据-->失败", e);
        } catch (IOException ioException) {
            flag = false;
            log.error("设置properties文件数据-->失败", ioException);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    log.error("设置properties文件数据-->关闭数据源失败", e);
                }
            }
        }
        return flag;
    }
}
